from .routes import admin_bp
from celery.result import AsyncResult
from flask import jsonify


# 查看任务结果
@admin_bp.route("/get_result/<uuid>/", methods=["GET"])
def get_result(uuid):
    from celery_worker import celery
    result = AsyncResult(id=uuid, app=celery)
    if result.successful():
        result = result.get()
        print(result)
    elif result.failed():
        result = "任务失败"
    elif result.status == "PENDING":
        result = "任务等待中被执行"
    elif result.status == "RETRY":
        result = "任务异常后正在重试"
    elif result.status == "STARTED":
        result = "任务已经开始被执行"
    return jsonify({"code": 0, "msg": "success", "data": result})
